package com.example.ffepeg.ffeAdder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class Add {
    private static final Logger log = LoggerFactory.getLogger(Add.class);
    //添加字幕list
    public static void main(String[] args) {
        String filePath = "src/main/resources/file/example.srt";
        clearSRTFile(filePath);
      /*
        // 创建一个列表来保存所有的字幕条目
        List<String> subtitles = new ArrayList<>();
        // 动态添加多个字幕条目
        addSubtitle(subtitles, 1, "00:00:20,000", "00:00:24,000", "This is the first subtitle.");
        addSubtitle(subtitles, 2, "00:00:25,000", "00:00:29,000", "This is the second subtitle.");
        addSubtitle(subtitles, 3, "00:00:30,000", "00:00:34,000", "This is the third subtitle.");

        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
            for (String subtitle : subtitles) {
                writer.write(subtitle);
                writer.newLine(); // 确保每个字幕条目之间有空行
            }
            System.out.println("所有字幕已成功写入到 " + filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }*/

        deleteFileContent("src/main/resources/file");


    }

    public static void deleteFileContent(String path) {
        File filePath = new File(path);
        // 检查文件是否存在且是一个文件而不是目录
        if (filePath.exists() && filePath.isFile()) {
            boolean isDeleted = filePath.delete();
            if (isDeleted) {
                log.info("文件删除成功: {}",path);
            } else {
                log.info("文件删除失败: {}",path);
            }
        } else if (!filePath.exists()) {
            log.info("文件不存在: {}", path);
        } else {
            log.info("无法删除，指定路径是一个文件夹而非文件");
        }
    }

    // 辅助方法用于构建并添加单个字幕条目
    private static void addSubtitle(List<String> subtitles, int index, String startTime, String endTime, String text) {
        StringBuilder subtitle = new StringBuilder();
        subtitle.append(index).append("\n")
                .append(startTime).append(" --> ").append(endTime).append("\n")
                .append(text).append("\n");
        subtitles.add(subtitle.toString());
    }

    private static void clearSRTFile(String file){
        try {
            Files.write(Paths.get(file), "".getBytes());
            System.out.println("清空example.srt文件成功");
        } catch (IOException e) {
            System.out.println("清空example.srt文件失败");
        }
    }
}
